<?php
// 应用公共文件
use  \think\facade\Response;
/**
 *  获取状态码描述
 *  苏鹏
 *  2023/03/20
 * code 状态码
 */
if (!function_exists('msg_code')) {

    function msg_code(int $code):string
    {
        $result = (new \app\admin\model\MessageCode())->getAdminModelCache('set');
        //多语言切换
        if(request()->header('lang') == 'en_us'){
            return empty($result[$code]['en_us']) ? '' : $result[$code]['en_us'];
        }
        return empty($result[$code]['zh_cn']) ? '' : $result[$code]['zh_cn'];
    }
}

/**
 * 获取系统配置缓存
 * 苏鹏
 * 2023/03/20
 * name 缓存标识
 */
if (!function_exists('get_config_cache')) {

    function get_config_cache(string $name)
    {
        $result = (new \app\admin\model\Config())->getAdminModelCache('set');
        //区分开发环境，生成环境
        if(!empty($result[$name]['distinguish']))
        {
            if(env('IS_PRODUCE') == false)
            {
                return $result[$name]['debug'];
            }else{
                return $result[$name]['produce'];
            }
        }else{
            return $result[$name];
        }
    }
}


/**
 * 生成密码
 * password 密码
 * return 加密后的字符串
 */
if (!function_exists('password')) {
    function password(string $password):string
    {
        // 生成加密后的密码，使用默认 PASSWORD_BCRYPT 加密算法
        return password_hash($password, PASSWORD_DEFAULT);
    }
}

/**
 * 验证密码
 * password 密码
 * salt 数据库保存的盐值
 */
if (!function_exists('verify_password')) {
    function verify_password(string $password, string $salt):bool
    {
        return password_verify($password, $salt);
    }
}


if (!function_exists('re_error_json')) {

    function re_error_json(string $msg = '', int $code = 500, array $data = [])
    {

        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Credentials:true');
        header('Access-Control-Allow-Methods:GET, POST, PATCH, PUT, DELETE');
        header('Access-Control-Allow-Headers:Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With');
        $msg = $msg ?: msg_code($code);
        $param = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];
        echo json_encode($param, JSON_UNESCAPED_UNICODE);
        die;
    }
}


if (!function_exists('re_success_json')) {
    function re_success_json(array $data = [], int $code = 200, string $msg = '')
    {
        $msg = $msg ?: msg_code($code);
        $param = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];
        $header = [
            "Access-Control-Allow-Origin" => "*",
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, DELETE',
            'Access-Control-Allow-Headers'     => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
        ];
       return json($param)->header($header);
    }
}

/**
 * redis 实例
 */
if (!function_exists('redis')) {
    function redis($num = 0)
    {
        $redis_config = get_config_cache('redis');
        $redis = new \Redis;
        $redis->connect($redis_config['host'], $redis_config['port']);
        $redis->auth($redis_config['password']);
        $redis->select($num);
        return $redis;
    }
}



/**
 * @param $url
 * Desc: 返回带域名的url
 * User: 苏鹏
 * Date: 2023/8/31
 * Time: 13:02
 * @return mixed|string
 */
if (!function_exists('return_upload_url')) {
    function return_upload_url($url)
    {
        if (empty($url)) {
            return $url;
        }
        //判断是否http/https开头，如果有直接返回
        $rule = '/^http:\/\//';
        $rules = '/^https:\/\//';
        preg_match($rule, $url, $match);
        preg_match($rules, $url, $matches);

        if (!empty($match[0]) || !empty($matches[0])) {
            return $url;
        } else {
            //判断第一个字符是否是‘.’
            if(substr($url, 0, 1) == '.'){
                $url = substr($url, 1);
            }
            //判断第一个字符是否是‘/’ 如果不是添加目录分隔符
            if (substr($url, 0, 1) == '/') {
                return get_config_cache('domain')['admin_domain'] . $url;
            } else {
                return get_config_cache('domain')['admin_domain'] . DIRECTORY_SEPARATOR . $url;
            }
        }
    }
}

/**
 * gateway 组发
 */
if (!function_exists('gateway_to_group')) {
    function gateway_to_group(string $group_name, array $data)
    {
        $config = get_config_cache('gateway_config');
        $gateway = (new \GatewayClient\Gateway);
        $gateway::$registerAddress = $config['gateway_register_address'];
        $res = $gateway::sendToGroup($config['gateway_to_group_prefix'].$group_name, json_encode($data));
    }
}




